﻿
namespace HSS.ComponentModel
{
	#region Using Directives
	using System;
	using System.ComponentModel;
	using System.Globalization;
	#endregion

	#region TimeSpanConverter
	/// <summary>
	/// Provides a type converter to convert TimeSpan objects to and from other representations.
	/// </summary>
	public class TimeSpanConverter : TypeConverter
	{
		/// <summary>
		/// Gets a value indicating whether this converter can convert an object in the given source type to a TimeSpan using the specified context.
		/// </summary>
		/// <param name="context">An ITypeDescriptorContext that provides a format context. </param>
		/// <param name="sourceType">A Type that represents the type you wish to convert from.</param>
		/// <returns>true if this converter can perform the conversion; otherwise, false.</returns>
		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
		{
			return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType));
		}
		/// <summary>
		/// Converts the given object to a TimeSpan.
		/// </summary>
		/// <param name="context">An ITypeDescriptorContext that provides a format context.</param>
		/// <param name="culture">An optional CultureInfo. If not supplied, the current culture is assumed.</param>
		/// <param name="value">The Object to convert.</param>
		/// <returns>An Object that represents the converted value.</returns>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")]
		public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
		{
			if (value is string)
			{
				string s = ((string)value).Trim();
				try
				{
					return TimeSpan.Parse(s, System.Globalization.CultureInfo.CurrentCulture);
				}
				catch (FormatException)
				{
					throw new FormatException();
				}
			}
			return base.ConvertFrom(context, culture, value);
		}
		/// <summary>
		/// Gets a value indicating whether this converter can convert an object to the given destination type using the context.
		/// </summary>
		/// <param name="context">An ITypeDescriptorContext that provides a format context.</param>
		/// <param name="destinationType">A Type that represents the type you wish to convert to.</param>
		/// <returns>true if this converter can perform the conversion; otherwise, false.</returns>
		public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
		{
			return ((destinationType == typeof(string)) || base.CanConvertTo(context, destinationType));
		}
		/// <summary>
		/// Converts the given object to another type.
		/// </summary>
		/// <param name="context">A formatter context.</param>
		/// <param name="culture">The culture into which value will be converted.</param>
		/// <param name="value">The object to convert.</param>
		/// <param name="destinationType">The type to convert the object to.</param>
		/// <returns>The converted object.</returns>
		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
		{
			if (destinationType == typeof(string))
			{
				if (value is TimeSpan)
					return ((TimeSpan)value).ToString();
			}
			return base.ConvertTo(context, culture, value, destinationType);
		}
	}
	#endregion
}